package cn.hd.userService.service.role.impl;

import cn.hd.commonService.entity.SysRole;
import cn.hd.commonService.entity.SysUser;
import cn.hd.commonService.entity.SysUserRole;
import cn.hd.commonService.exception.CustomException;
import cn.hd.userService.mapper.role.SysUserRoleMapper;
import cn.hd.userService.service.role.ISysUserRoleService;
import cn.hd.userService.service.role.SysRoleService;
import cn.hd.userService.service.user.ISysUserService;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hd
 * @since 2022-06-26
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private ISysUserService userService;
    @Autowired
    private SysRoleService roleService;



    /**
     * 根据用户查询关联的系统角色
     * @param userId
     * @return
     */
    public List<SysUserRole> getSysUserRoleByUserId(Integer userId,Integer roleId) {
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        if (!StrUtil.isEmptyIfStr(userId)){
            wrapper.eq(SysUserRole::getUserId,userId);
        }
        if (!StrUtil.isEmptyIfStr(roleId)){
            wrapper.eq(SysUserRole::getRoleId,roleId);
        }
        return sysUserRoleMapper.selectList(wrapper);
    }

    public void saveRelation(String roleIds,String userIds){
        //根据用户删除角色和用户之间的关联关系
        List<Integer> userList = Convert.toList(Integer.class, userIds);
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysUserRole::getUserId,userList);
        sysUserRoleMapper.delete(wrapper);
        List<Integer> roleList = Convert.toList(Integer.class, roleIds);
        List<SysUserRole> result = new ArrayList<>();
        for (Integer userId : userList) {
            SysUser user = userService.getById(userId);
            Optional.ofNullable(user).orElseThrow(()->new CustomException("人员信息有误"));
            for (Integer roleId : roleList) {
                SysRole sysRole = roleService.getById(roleId);
                Optional.ofNullable(sysRole).orElseThrow(()->new CustomException("角色信息有误"));
                result.add(new SysUserRole(roleId,userId));
            }
        }
        //批量保存
        saveBatch(result);
    }

    @Override
    public List<SysUserRole> getSysUserRoleByUserId(int userId) {
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId,userId);
        return sysUserRoleMapper.selectList(wrapper);
    }
}
